const http = require('../../utils/http.js');
const app = getApp();
const regeneratorRuntime = require("../../utils/regenerator-runtime/runtime-module.js");
const { $Toast } = require('../../dist/base/index');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderStatus: false,
		orderDtail: []
	},

  secondToDate(result) {
    var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
    var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
    var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
    return result = h + ":" + m + ":" + s;
  },

	//获取订单详情
	getOrderDetails() {
		var orderId = this.data.orderId;
		http.fetchApi('procedure_t', {
				p: 'R2002011|' + orderId
			})
			.then(res => {
				if (res.data.code == 1) {
					var orderDtail = res.data.results[0];
          if (orderDtail.date_sd){
            var date_sd = orderDtail.date_sd,
              h = date_sd.split(':')[0] / 1,
              m = date_sd.split(':')[1] / 1,
              s = date_sd.split(':')[2] / 1;
            console.log(h, m, s);
            var hm = s + m * 60 + h * 60 * 60;
            console.log(hm)
            var timer = setInterval(() => {
              hm--;
              hm < 0 ? hm = 0 : '';
              var hms = this.secondToDate(hm)
              // console.log(hms)
              this.setData({
                hms:hms 
              })
            }, 1000)
            this.setData({
              timer,
            })
          }else{
            this.setData({
              hms:''
            })
          }
          
					this.setData({
						orderDtail,
            oid: orderDtail.oid,  
            studentPhone: orderDtail.yhdh,
            kefuPhone: orderDtail.sjsjh
					})
          this.getShansong();
				}
			})
	},

  //获取闪送订单
  getShansong() {
    var oid = this.data.oid;
    http.fetchApi('procedure_t', { p: `R2002009|${oid}` }).then(res => {
      if (res.data.code == 1) {
        var jiadanArr = res.data.results;
        console.log(jiadanArr);
        this.setData({
          jiadanArr
        })
      }
    })
  },

	toRefund() {
		wx.navigateTo({
			url: '/pages/refund/refund?orderId='+this.data.orderId
		})
	},

  //骑手确认送达
  confirmService(e){
    var orderId = this.data.orderId;
    var formId = e.detail.formId;
    var uid = app.globalData.uid;
    console.log('R2013001|' + formId + '|' + uid)
    http.fetchApi('procedure_t', {
      p: 'R2013001|' + formId + '|' + uid
    })
    wx.showModal({
      title: '提示',
      content: '是否确认送达?',
      success:(res1)=> {
        if (res1.confirm) {
          http.fetchApi('p_order_ddsd', { p: `R2007008|${orderId}` }).then(res => {
            if (res.data.code == 1) {
              var codes = res.data.results[0].codes;
              if (codes == 1) {
                // http.showToast('确认成功' + '', '', '').then(() => {
                //   this.getOrderDetails();
                // })
                $Toast({
                  content: '确认成功',
                  type: 'success'
                });
                this.getOrderDetails();
              } else {
                
                $Toast({
                  content: '服务器出错!',
                  type: 'error'
                });
              }
            }
          })
        } else if (res1.cancel) {
         
        }
      }
    })
    
  },
  //联系同学
  cnacatStudent(){
   
    if (this.data.studentPhone){
      wx.makePhoneCall({
        phoneNumber: this.data.studentPhone,
      })
    }else{
     
      $Toast({
        content: '暂无学生电话!',
        type: 'error'
      });
    }
    
  },
  //联系客服
  concatKefu(){
    wx.makePhoneCall({
      phoneNumber: this.data.kefuPhone,
    })
  },

  //骑手手动出库
  chuku(e){
    var orderId = this.data.orderId;
    var uid = app.globalData.uid;
    var formId = e.detail.formId;
    console.log('R2013001|' + formId + '|' + uid)
    http.fetchApi('procedure_t', {
      p: 'R2013001|' + formId + '|' + uid
    })
    wx.showModal({
      title: '提示',
      content: '确定出库?',
      success:(res1)=> {
        if (res1.confirm) {
          http.fetchApi('p_order_qsck', { p: `R2012001|${orderId}` }).then(res => {
            if (res.data.code == 1) {
              if (res.data.results[0].codes == 1) {
                $Toast({
                  content: '出库成功',
                  type: 'success'
                });
                this.getOrderDetails();
              }
            }
          })
        } else if (res1.cancel) {
          
        }
      }
    })

    
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var orderId = options.orderId;
		this.setData({
			orderId
		})

		this.getOrderDetails();
   
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
    clearInterval(this.data.timer)
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
